﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections.Specialized;

namespace MS.Msn.MediaSolutions.Common.Web
{
    /// <summary>
    /// methods to control session, cookie, question string and remain states
    /// </summary>
    public static class States
    {
        #region Session

        /// <summary>
        /// set session object
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        /// <param name="key">session key</param>
        /// <param name="val">session value</param>
        /// <returns>session value</returns>
        public static T SetSession<T>(string key, T val)
        {
            if (!object.Equals(HttpContext.Current, null))
            {
                if (val != null || string.IsNullOrEmpty(val.ToString()))
                {
                    HttpContext.Current.Session[key] = val;
                }
                else
                {
                    if (HttpContext.Current.Session[key] != null)
                    {
                        HttpContext.Current.Session.Remove(key);
                    }
                }
            }

            return val;
        }

        /// <summary>
        /// get session object
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        /// <param name="obj">object instance</param>
        /// <param name="key">session key</param>
        /// <returns>session value</returns>
        public static T GetSession<T>(T obj, string key)
        {
            T rtval = obj;

            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Session[key] != null)
                {
                    rtval = (T)HttpContext.Current.Session[key];
                    HttpContext.Current.Session[key] = rtval;
                }
            }

            return rtval;
        }

        #endregion

        #region Cookie

        /// <summary>
        /// set cookie
        /// </summary>
        /// <param name="key">cookie key</param>
        /// <param name="val">cookie value</param>
        /// <param name="expiresTime">cookie expires time</param>
        /// <returns>cookie value</returns>
        public static string SetCookie(string key, string val, DateTime expiresTime)
        {
            return SetCookie(key, val, expiresTime, null);
        }

        /// <summary>
        /// set cookie
        /// </summary>
        /// <param name="key">cookie key</param>
        /// <param name="val">cookie value</param>
        /// <param name="expiresTime">cookie expires time</param>
        /// <param name="resp">response</param>
        /// <returns>cookie value</returns>
        public static string SetCookie(string key, string val, DateTime expiresTime, HttpResponse resp)
        {
            if (resp == null && HttpContext.Current != null)
            {
                resp = HttpContext.Current.Response;
            }

            if (resp != null)
            {
                if (!string.IsNullOrEmpty(val))
                {
                    HttpCookie ck = new HttpCookie(key, val);
                    ck.Expires = expiresTime;
                    resp.Cookies.Set(ck);
                }
                else if (resp.Cookies[key] != null)
                {
                    resp.Cookies[key].Expires = new DateTime(2000, 1, 1);
                }
            }
            return val;
        }

        /// <summary>
        /// get cookie
        /// </summary>
        /// <param name="key">cookie key</param>
        /// <returns>cookie value</returns>
        public static string GetCookie(string key)
        {
            return GetCookie(key, null);
        }

        /// <summary>
        /// get cookie
        /// </summary>
        /// <param name="key">cookie key</param>
        /// <param name="rqst">request</param>
        /// <returns>cookie value</returns>
        public static string GetCookie(string key, HttpRequest rqst)
        {
            string rtval = string.Empty;

            if (rqst == null && HttpContext.Current != null)
            {
                rqst = HttpContext.Current.Request;
            }

            if (rqst != null)
            {
                if (rqst.Cookies[key] != null)
                {
                    rtval = rqst.Cookies[key].Value;
                }
            }

            return rtval;
        }

        #endregion

        #region Query string

        /// <summary>
        /// set parameter in query string
        /// </summary>
        /// <param name="uLoc">target url</param>
        /// <param name="pName">parameter name</param>
        /// <param name="pVal">parameter value</param>
        /// <returns>url</returns>
        public static string SetQueryParam(string uLoc, string pName, string pVal)
        {
            if (!string.IsNullOrEmpty(uLoc))
            {
                string temp = string.Empty;
                string rqLoc = uLoc.ToLower(Apps.DefaultCulture);
                int index = rqLoc.LastIndexOf(pName, StringComparison.OrdinalIgnoreCase);

                if (index > 0)
                {
                    temp = rqLoc.Substring(index, rqLoc.Length - index);
                    int index1 = temp.IndexOf("&", StringComparison.OrdinalIgnoreCase);
                    if (index1 > 0)
                        rqLoc = rqLoc.Remove(index, index1 + 1);
                    else
                        rqLoc = rqLoc.Remove(index, rqLoc.Length - index);
                }

                if (rqLoc.IndexOf('?') < 0)
                {
                    rqLoc = rqLoc + "?" + pName + "=";
                }
                else
                {
                    if (rqLoc[rqLoc.Length - 1] == '?')
                    {
                        rqLoc += pName + "=";
                    }
                    else
                    {
                        if (rqLoc[rqLoc.Length - 1] != '&')
                        {
                            rqLoc += '&';
                        }
                        rqLoc += pName + "=";
                    }
                }

                return rqLoc + pVal;
            }
            else
            {
                throw new ArgumentNullException(Apps.GetArgNullExString("uLoc"));
            }
        }

        /// <summary>
        /// get query string param
        /// </summary>
        /// <param name="uLoc">target url</param>
        /// <param name="pName">parameter name</param>
        /// <returns>parameter value</returns>
        public static string GetQueryParam(string uLoc, string pName)
        {
            string rtval = string.Empty;

            if (!string.IsNullOrEmpty(uLoc))
            {
                NameValueCollection nv = HttpUtility.ParseQueryString(uLoc);

                if (nv != null)
                {
                    rtval = nv[pName];
                }
            }

            return rtval;
        }

        #endregion
    }
}